package com.ruoyi.warehousing.controller;


import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.product.domain.Product;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.sale.mapper.SaleGoodsMapper;
import com.ruoyi.slow.domain.Slow;
import com.ruoyi.slow.service.SlowService;
import com.ruoyi.warehousing.domain.WarehouseStatisticsVo;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingMapper;
import com.ruoyi.warehousing.service.WarehousingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 库存记录Controller
 *
 * @author zjh
 * @date 2022-06-01
 */
@RestController
@RequestMapping("/warehousing")
public class WarehousingController extends BaseController {

    @Autowired
    WarehousingMapper warehousingMapper;

    private String prefix = "warehousing";

    @Autowired
    private WarehousingService warehousingService;
    @Autowired
    private SlowService slowService;

    @Autowired
    private SaleGoodsMapper saleGoodsMapper;

    public static void main(String[] args) {
        double a = 700;
        double b = 508;
        System.out.println(a / b);
    }

    /**
     * 查询产品列表
     */
    @RequiresPermissions("business:warehousing:view")
    @GetMapping()
    public String operlog() {
        return prefix + "/warehousing";
    }

    /**
     * 查询产品列表
     */
    // @RequiresPermissions("business:warehousing:list")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(@RequestParam(value = "productName", required = false) String productName, @RequestParam(value = "productType", required = false) String productType) {
        // Product product = new Product();
        // product.setProductName(productName);
        // product.setProductType(productType);
        // startPage();
        // List<Product> list = warehousingService.selectProductToWar(product);
        // return getDataTable(list);

        WarehouseStatisticsVo vo = new WarehouseStatisticsVo();
        vo.setProductName(productName);
        vo.setProductType(productType);
        if (null == vo.getDeptIds()) {
            vo.setDeptIds(Collections.singletonList(SecurityUtils.getDeptId()));
        }
        vo.setComparisonPeriod(180L);
        vo.setArrivalPeriod(60L);
        // startPage();
        List<WarehouseStatisticsVo> vos = warehousingService.selectProductToWar_new(vo);
        return AjaxResult.success(vos);
    }

    @GetMapping("/SaleTendency")
    @ResponseBody
    public TableDataInfo SaleTendency() {
        //  startPage();
        List<SaleGoodsModel> saleGoodsModelList = warehousingService.selectTendency();
        return getDataTable(saleGoodsModelList);
    }


    @PostMapping("/selectCycle")
    @ResponseBody
    public SaleGoodsModel selectCycle(@RequestBody SaleGoodsModel saleGoodsModel) {
        SaleGoodsModel saleGoodsModel1 = warehousingService.selectCycle(saleGoodsModel);
        return saleGoodsModel1;

    }

    /**
     * 修改公司培训记录
     */
    @GetMapping("/entryEdit")
    public String edit(Long productId, String productType, ModelMap mmap) {
        mmap.put("productType", productType);
        mmap.put("productId", productId);
        return prefix + "/edit";
    }

    /**
     * 修改公司培训记录
     */
    @RequiresPermissions("business:warehousingTl:view")
    @GetMapping("/detialsWar")
    public String detialsWar(String keyIm, String withNum, ModelMap mmap) {
        mmap.put("keyIm", keyIm);
        mmap.put("withNum", withNum);
        return prefix + "/detialsWar";
    }

    /**
     * 查询库存记录
     */
    @RequiresPermissions("business:warehousingTl:list")
    @PostMapping("/selectDetialsWar")
    @ResponseBody
    public TableDataInfo selectDetialsWar(String keyIm, ModelMap mmap) {
        startPage();
        List<Warehousing> list = warehousingService.selectWarehousingDe(keyIm);
        return getDataTable(list);
    }

    /**
     * 修改公司培训记录
     */
    @RequiresPermissions("business:warehousingSlow:view")
    @GetMapping("/slowDetail")
    public String slowDetail(String keyIm, String withNum, ModelMap mmap) {
        mmap.put("keyIm", keyIm);
        mmap.put("withNum", withNum);
        return prefix + "/slowDetail";
    }

    /**
     * 查询库存记录
     */
    @RequiresPermissions("business:warehousingSlow:list")
    @PostMapping("/selectSlowDetail")
    @ResponseBody
    public TableDataInfo selectSlowDetail() {
        List<Slow> list = slowService.selectSlowListWhichSpill(new Slow());
        return getDataTable(list);
    }

    /**
     * 查询库存记录
     */
    @GetMapping("/selectWarehousingDetail")
    @ResponseBody
    public TableDataInfo selectWarehousingDetail(@RequestParam(value = "productId", required = false) Long productId,
                                                 @RequestParam(value = "productType", required = false) String productType) {
        Warehousing warehousing = new Warehousing();
        warehousing.setProductId(productId);
        warehousing.setProductType(productType);
        List<Warehousing> list = warehousingService.selectWarehousingDetail(warehousing);
        return getDataTable(list);
    }

    /**
     * 查询库存记录列表
     */
    @PostMapping("/selectWarehousingRecordsDetail")
    @ResponseBody
    public TableDataInfo selectWarehousingRecordsDetail(String productId, String productType) {
        Warehousing warehousing = new Warehousing();
        warehousing.setProductId(Long.parseLong(productId));
        warehousing.setProductType(productType);
        List<Warehousing> list = warehousingService.selectWarehousingDetail(warehousing);
        return getDataTable(list);
    }

    /**
     * 修改库存记录
     */
    @PostMapping("/editByInput")
    @ResponseBody
    public AjaxResult editByInput(Long warehousingId, Integer warehousingNum) {
        Warehousing warehousing = new Warehousing();
        warehousing.setWarehousingId(warehousingId);
        warehousing.setWarehousingNum(warehousingNum);
        //查询产品库存信息
        Warehousing warehousing1 = warehousingService.selectWarehousing(warehousing);
        Integer num = warehousingNum - warehousing1.getWarehousingNum();
        if (num != 0) {
            WarehousingRecords warehousingRecords = new WarehousingRecords();
            warehousingRecords.setInventoryNum(num);
            warehousingRecords.setWarehousingId(warehousingId);
            warehousingRecords.setInventoryStatus("2");
            //插入入库库存记录信息
            warehousingService.insertWarehousingRecords(warehousingRecords);
        }
        //修改产品库存信息
        return toAjax(warehousingService.updateWarehousing(warehousing));
    }

    /**
     * 查询库存记录
     */
    @GetMapping("/selectWarehousingRecords")
    @ResponseBody
    public List<WarehousingRecords> selectWarehousingRecords(@RequestParam(value = "warehousingId", required = false) Long warehousingId) {
        startPage();
        WarehousingRecords warehousingRecords = new WarehousingRecords();
        warehousingRecords.setWarehousingId(warehousingId);
        List<WarehousingRecords> list = warehousingService.selectWarehousingRecords(warehousingRecords);
        for (WarehousingRecords warehousingRecords1 : list) {
            if (warehousingRecords1.getAllnum() == 0) {
                warehousingRecords1.setAllnum(warehousingRecords1.getInventoryNum());
            }
        }
        return list;
    }

}
